# models.py
from peewee import *

db = SqliteDatabase('students.db')

class BaseModel(Model):
    class Meta:
        database = db

class Student(BaseModel):
    sno = CharField(max_length=20)
    name = CharField(max_length=50)
    age = IntegerField()
    gender = CharField(max_length=10)
    major = CharField(max_length=50)

    def to_dict(self):
        return {
            'id': self.id,
            'sno': self.sno,
            'name': self.name,
            'age': self.age,
            'gender': self.gender,
            'major': self.major
        }

def init_db():
    db.connect(reuse_if_open=True)
    db.create_tables([Student])
    if Student.select().count() == 0:
        Student.create(
            sno="202305571104",
            name="张三",
            age=18,
            gender="男",
            major="通信工程"
        )
        Student.create(
            sno="202305571166",
            name="李四",
            age=20,
            gender="女",
            major="人工智能"
        )
        print("✨ 已插入张三和李四")
    else:
        print("✅ 数据库已存在记录，未重复插入")


